package com.diqye.db.event.impl;

import com.diqye.db.XDB;
import com.diqye.db.event.TableEventSave;
import com.diqye.db.model.XModel;
import com.diqye.log.Logger;
/**
 * 自动生ID
 *
 * @author 秦振龙
 * @date 2013-12-10
 *
 * <b>修改记录及说明：</b>
 * <ul>
 * <li>2013-12-10  秦振龙  创建</li>
 * </ul>
 */
public class AutoPrimary implements TableEventSave {
	Logger log = Logger.getLogger(this.getClass());
	public void after(XModel model) {
	}

	public void before(XModel model) {
		log.debug("自动递增ID");
		String idfield =  model.getTinfo().getPrimaryKey().get(0);
		if(model.get(idfield) != null){
			return;
		}
		XDB db = new XDB();
		Long id = db.findLong("select max(${0}) from ${1} ",idfield,model.getTinfo().getTablename());
		id = id == null ? 0l:id;
		model.set(idfield, id+1);
	}

}
